package com.ncmmall.application.securitys.navigation;

import com.ncmmall.domain.security.auth.Authority;
import com.ncmmall.domain.security.auth.AuthorityRepository;
import com.ncmmall.domain.security.auth.QAuthority;
import com.ncmmall.domain.security.nav.Navigation;
import com.ncmmall.domain.security.nav.NavigationRepository;
import com.ncmmall.domain.security.nav.QNavigation;
import com.ncmmall.domain.security.role.RoleAuthority;
import com.ncmmall.domain.security.role.RoleAuthorityRepository;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author linxs
 *         菜单管理
 */
@Service
public class NavigationApplication {

    @Autowired
    private NavigationRepository navRepository;

    @Autowired
    private AuthorityRepository authorityRepository;

    @Autowired
    private RoleAuthorityRepository roleAuthorityRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 添加菜单
     *
     * @param nav
     * @return
     */
    public Navigation create(Navigation nav) {
        List<String> permissions = Splitter.on("-").trimResults().omitEmptyStrings().splitToList(nav.getAuthority());
        navRepository.save(nav);

        for (String permission : permissions) {
            Authority authority = new Authority();
            authority.setName(permission);
            authority.setPermission(permission);
            authority.setNavigation(nav);
            authorityRepository.save(authority);

            RoleAuthority roleAuthority = new RoleAuthority();
            roleAuthority.setRoleId(nav.getRole().getId());
            roleAuthority.setAuthoritysId(authority.getId());
            roleAuthorityRepository.save(roleAuthority);
        }

        return nav;
    }

    /**
     * 修改菜单
     *
     * @param nav
     * @return
     */
    public Navigation modify(Navigation nav) {
        List<String> permissions = Splitter.on("-").trimResults().omitEmptyStrings().splitToList(nav.getAuthority());

        Navigation old = queryChannelService.findOne(QNavigation.navigation.id.eq(nav.getId()), Navigation.class);
        old.setHref(nav.getHref());
        old.setTitle(nav.getTitle());
        old.setIcon(nav.getIcon());
        old.setRank(nav.getRank());
        old.setSpread(nav.isSpread());
        old.setPage(nav.getPage());
        navRepository.save(old);

        List<Authority> authorities = queryChannelService.findAll(QAuthority.authority.navigation.id.eq(nav.getId()), Authority.class);
        for (Authority authority : authorities) {
            roleAuthorityRepository.delete(queryChannelService.findOne("select r from RoleAuthority r where r.roleId = :rid and r.authoritysId = :aid ", ImmutableMap.of("rid", old.getRole().getId(), "aid", authority.getId()), RoleAuthority.class));
            authorityRepository.delete(authority);
        }

        for (String permission : permissions) {
            Authority authority = new Authority();
            authority.setName(permission);
            authority.setPermission(permission);
            authority.setNavigation(nav);
            authorityRepository.save(authority);

            RoleAuthority roleAuthority = new RoleAuthority();
            roleAuthority.setRoleId(nav.getRole().getId());
            roleAuthority.setAuthoritysId(authority.getId());
            roleAuthorityRepository.save(roleAuthority);
        }


        return old;
    }

    /**
     * 删除菜单
     *
     * @param nav
     */
    public void remove(Navigation nav) {
        for (Navigation child : nav.getChildren()) {
            List<Authority> authorities = queryChannelService.findAll(QAuthority.authority.navigation.id.eq(child.getId()), Authority.class);
            for (Authority authority : authorities) {
                roleAuthorityRepository.delete(queryChannelService.findOne("select r from RoleAuthority r where r.roleId = :rid and r.authoritysId = :aid ", ImmutableMap.of("rid", child.getRole().getId(), "aid", authority.getId()), RoleAuthority.class));
                authorityRepository.delete(authority);
            }
            navRepository.delete(child);
        }

        List<Authority> authorities = queryChannelService.findAll(QAuthority.authority.navigation.id.eq(nav.getId()), Authority.class);
        for (Authority authority : authorities) {
            roleAuthorityRepository.delete(queryChannelService.findOne("select r from RoleAuthority r where r.roleId = :rid and r.authoritysId = :aid ", ImmutableMap.of("rid", nav.getRole().getId(), "aid", authority.getId()), RoleAuthority.class));
            authorityRepository.delete(authority);
        }
        nav.setChildren(null);
        navRepository.delete(nav);
    }

}
